#!/usr/bin/env python

"""
This module provide a tool for doing migration by shell line
@author Marinho Brandao
@creation 2007-05-25
"""

import sys, os
from yadsel import core, drivers
from yadsel.execution import *

try:
    import simplejson
except ImportError, e:
    pass

def print_header():
    print "Yadsel Tool // http://code.google.com/p/yadsel/\n"

def print_help():
    temp = [
            "Invalid syntax. You must declare required parameters, like below:",
            "",
            "  driver=<drive_name>\n",
            "  path=<version_files_path>\n",
            "  dsn=<dsn>\n",
            "  user=<username>\n",
            "  pass=<password>\n\toptional. Default: empty\n",
            "  from=<current_version>\n\toptional. Default: latest version of history control\n",
            "  to=<new_version_number>\n\toptional. Default: latest available version\n",
            "  action=[%s]\n\toptional. Default: %s\n" %( ''.join([m+'|' for m in AVAILABLE_ACTIONS])[:-1], core.ACTION_UP ),
            "  mode=[%s]\n\toptional. Default: %s\n" %( ''.join([m+'|' for m in AVAILABLE_MODES])[:-1], MODE_HIDDEN ),
            "  test\n\toptional. Turns off persistent changings on database\n",
            "  history\n\toptional. Turns on history control on database",
            "  silent\n\toptional. Only shows error messages without stop upgrade/downgrade process",
            "  log\n\toptional. Register the log of changes maked on a table called 'yadsel_log'",
           ]

    print ''.join([l+"\n" for l in temp])


if __name__ == '__main__':
    print_header()

    versions_path = dsn = user = current_version = None
    action = core.ACTION_UP
    new_version = 0
    passwd = ''
    mode = MODE_HIDDEN
    log = silent = test = history = False

    if len(sys.argv) == 1 or sys.argv[1].find('=') == -1:
        if os.path.isfile(DEFAULT_YADSEL_FILENAME):
            ns = len(sys.argv) == 2 and sys.argv[1] or None
            do_from_file(DEFAULT_YADSEL_FILENAME, ns)
            sys.exit(0)

    for arg in sys.argv:
        if arg.startswith('path='):
            versions_path = arg.split('=')[1]
        elif arg.startswith('driver='):
            driver_type = arg.split('=')[1].lower()
        elif arg.startswith('dsn='):
            dsn = arg.split('=')[1]
        elif arg.startswith('user='):
            user = arg.split('=')[1] or action
        elif arg.startswith('pass='):
            passwd = arg.split('=')[1]
        elif arg.startswith('action='):
            action = arg.split('=')[1] or passwd
        elif arg.startswith('from='):
            current_version = int(arg.split('=')[1])
        elif arg.startswith('to='):
            new_version = int(arg.split('=')[1]) or new_version
        elif arg.startswith('mode='):
            mode = arg.split('=')[1]
        elif arg == 'test':
            test = True
        elif arg == 'history':
            history = True
        elif arg == 'silent':
            silent = True
        elif arg == 'log':
            log = True

    if not versions_path or not driver_type:
        print_help()
        sys.exit(1)

    # Does the (up|down)grade
    do(versions_path=versions_path,
       driver_type=driver_type,
       dsn=dsn,
       action=action,
       user=user,
       passwd=passwd,
       current_version=current_version,
       new_version=new_version,
       mode=mode,
       test=test,
       history=history,
       silent=silent,
       log=log,
       )

